package com.fcscs.cservice.client.main.configuration.inroomdining;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.fcscs.core.gwt.common.AsyncCallbackListener;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.CoreUtil;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.ui.CenteredInfo;
import com.fcscs.core.gwt.common.ui.FcsFields.HighLightIconButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel.ModelSelectListener;
import com.fcscs.core.gwt.common.ui.SimpleGridPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.MessageBoxTools;
import com.fcscs.cservice.client.common.controller.InRoomServiceController;
import com.fcscs.cservice.client.common.model.configuration.inroomdining.BillOptionModelData;
import com.fcscs.cservice.client.common.model.configuration.reservation.PropertyIdNameModelData;
import com.fcscs.cservice.domain.BillOption;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class BillOptionGrid extends SimpleGridPanel<BillOptionModelData> implements
		FormSubmitListener<BillOption>, ModelSelectListener<BillOptionModelData> {

	private static BillOptionGrid INSTANCE;
	private BillOptionDialog billOptionDialog;
	private BillOptionModelData currentBillOptionModelData;
	private PropertyIdNameModelData currentProperty;

	public BillOptionGrid(String heading, ModelSelectListener<BillOptionModelData> l) {
		super(heading, l, (Object) null);
		setCollapsible(false);
		registerListeners(this);
		setSize(650, 520);
	}

	public static BillOptionGrid getInstance(String title,
			ModelSelectListener<BillOptionModelData> modelSelectListener,PropertyIdNameModelData currentProperty) {
		if (null == INSTANCE) {
			INSTANCE = new BillOptionGrid(title, modelSelectListener);
		}
		INSTANCE.currentProperty=currentProperty;
		return INSTANCE;
	}

	@Override
	public void onModelClick(BillOptionModelData model) {
		
	}
	@Override
	public void onModelDoubleClick(BillOptionModelData model) {
		showEditDeviceDialog(model);
	}

	@Override
	public void onModelEnterPress(BillOptionModelData model) {
		showEditDeviceDialog(model);
	}

	@Override
	public void formSubmitCallBack(FcsFormPanel<BillOption> form, SubmitType submitType) {
		billOptionDialog.getFormPanel().populateEntity(currentBillOptionModelData.getEntity());
		this.modifyAdvertisement(currentBillOptionModelData.getEntity());
	}
	private void modifyAdvertisement(final BillOption entity) {
		InRoomServiceController.getINSTANCE().saveBillOption(entity, new AsyncCallbackListener<BillOption>(){

			@Override
			public void onLogin(String username) {
				InRoomServiceController.getINSTANCE().saveBillOption(entity,this);
			}

			@Override
			public void onFailure(Throwable caught) {
				MessageBoxTools.messageAlertSystemError();
			}

			@Override
			public void onSuccess(BillOption result) {
				if (result != null) {
					replaceModel(currentBillOptionModelData, new BillOptionModelData(result));
					billOptionDialog.hide();
					CenteredInfo.displayCompleted(CoreConstants.CORE_CONSTANTS.edited());
				} else {
					MessageBoxTools.messageAlertSystemError();
				}
			}
			
		});
	}

	@Override
	protected ColumnModel createColumnConfig(Object... params) {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();

		ColumnConfig cc = CoreUtil.createNonSortableColumnConf(CServiceConstants.NAME,
			CoreConstants.CORE_MESSAGES.operation(CServiceConstants.CSERVICE_CONSTANTS.billOption(),CoreConstants.CORE_CONSTANTS.name()),160);
		columns.add(cc);
		
		columns.add(CoreUtil.createNonSortableColumnConf(CServiceConstants.NAME_SEC_LANG,
				CoreConstants.CORE_MESSAGES.operation(CServiceConstants.CSERVICE_CONSTANTS.billOption(),CoreConstants.CORE_CONSTANTS.secondaryName()), 160));
		
		cc = CoreUtil.createNonSortableColumnConf(CoreConstants.ACTION,
				CoreConstants.CORE_CONSTANTS.action(), 160);
		cc.setRenderer(new GridCellRenderer<BillOptionModelData>() {
			public Object render(final BillOptionModelData model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore<BillOptionModelData> store,
					final Grid<BillOptionModelData> grid) {
				HorizontalPanel hr = new HorizontalPanel();
				hr.setHorizontalAlign(HorizontalAlignment.LEFT);

				HighLightIconButton ib = createEditBtn(model);
				HighLightIconButton id = createDeleteBtn(model, store);
				ib.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "3px");
				id.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "3px");
				ib.setId("BillOptionGrid_edit_button");
				id.setId("BillOptionGrid_dele_button");
				hr.add(ib);
				hr.add(id);
				return hr;
			}
		});
		columns.add(cc);
		ColumnModel columnModel = new ColumnModel(columns);
		return columnModel;
	}

	private HighLightIconButton createEditBtn(final BillOptionModelData model) {
		HighLightIconButton iconBtn = new HighLightIconButton(ShareGWTConstants.CSS_EDIT_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
			@Override
			public void componentSelected(IconButtonEvent ce) {
				showEditDeviceDialog(model);
			}
		});
		iconBtn.setId("BillOptionGrid_create_editbtn_id");
		iconBtn.setTitle(CoreConstants.CORE_CONSTANTS.edit());
		return iconBtn;
	}
	
	private void showEditDeviceDialog(BillOptionModelData model) {
		billOptionDialog = BillOptionDialog.getInstance(false,this,currentProperty);
		billOptionDialog.getFormPanel().updateForm(model.getEntity());
		billOptionDialog.show();
	}

	private HighLightIconButton createDeleteBtn(final BillOptionModelData model,
			final ListStore<BillOptionModelData> store) {
		HighLightIconButton deleBtn = new HighLightIconButton(CoreConstants.CSS_DELETE_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
					@Override
					public void componentSelected(IconButtonEvent ce) {
						MessageBox.confirm(CoreConstants.CORE_MESSAGES.operation(
								CoreConstants.CORE_CONSTANTS.delete(),
								ShareGWTConstants.SHARE_GWT_CONSTANTS.device()),
								ShareGWTConstants.SHARE_GWT_MESSAGES.whetherOperation(
										CoreConstants.CORE_CONSTANTS.delete().toLowerCase(), model
												.getEntity().getName()),
								new Listener<MessageBoxEvent>() {

									@Override
									public void handleEvent(MessageBoxEvent be) {
										Button btn = be.getButtonClicked();
										if (Dialog.YES.equals(btn.getItemId())) {
											deleteBillOption(model, store);
										}
									}
								});
					}
				});
		deleBtn.setId("BillOptionGrid_create_deleteBtn_id");
		deleBtn.setTitle(CoreConstants.CORE_CONSTANTS.delete());
		return deleBtn;
	}
	private void deleteBillOption(
			final BillOptionModelData model,
			final ListStore<BillOptionModelData> store) {
		InRoomServiceController.getINSTANCE().deleteBillOption(model.getEntity(), new AsyncCallbackListener<Boolean>(){

			@Override
			public void onLogin(String username) {
				InRoomServiceController.getINSTANCE().deleteBillOption(model.getEntity(),this);
			}

			@Override
			public void onFailure(Throwable caught) {
				MessageBoxTools.messageAlertSystemError();	
			}

			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					store.remove(model);
					CenteredInfo.displayCompleted(CoreConstants.CORE_CONSTANTS
							.deleted());
				} else {
					MessageBox.alert(
							CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.delete(),
									CServiceConstants.CSERVICE_CONSTANTS.billOption()),
							ShareGWTConstants.SHARE_GWT_MESSAGES.operationFail(
									CoreConstants.CORE_CONSTANTS.delete(), model.getEntity().getName()),
							null);
				}
			}
			
		});
	}
}
